/**
 *
 */
package com.zhuoyue.zhtj.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zhuoyue.zhtj.common.ServerResponse;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.eit.eitsite.common.config.Global;
import com.eit.eitsite.common.persistence.Page;
import com.eit.eitsite.common.web.BaseController;
import com.eit.eitsite.common.utils.StringUtils;
import com.zhuoyue.zhtj.entity.Dmxx;
import com.zhuoyue.zhtj.service.DmxxService;

import java.util.List;

/**
 * 弹幕信息Controller
 * @author hss
 * @version 2017-08-17
 */
@Controller
@RequestMapping(value = "${adminPath}/zhtj/dmxx")
public class DmxxController extends BaseController {

	@Autowired
	private DmxxService dmxxService;
	
	@ModelAttribute
	public Dmxx get(@RequestParam(required=false) String id) {
		Dmxx entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = dmxxService.get(id);
		}
		if (entity == null){
			entity = new Dmxx();
		}
		return entity;
	}
	
	@RequiresPermissions("zhtj:dmxx:view")
	@RequestMapping(value = {"list", ""})
	public String list(Dmxx dmxx, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Dmxx> page = dmxxService.findPage(new Page<Dmxx>(request, response), dmxx); 
		model.addAttribute("page", page);
		return "zhuoyue/zhtj/dmxxList";
	}

	@RequiresPermissions("zhtj:dmxx:view")
	@RequestMapping(value = "form")
	public String form(Dmxx dmxx, Model model) {
		model.addAttribute("dmxx", dmxx);
		return "zhuoyue/zhtj/dmxxForm";
	}

	@RequiresPermissions("zhtj:dmxx:edit")
	@RequestMapping(value = "save")
	public String save(Dmxx dmxx, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, dmxx)){
			return form(dmxx, model);
		}
		dmxxService.save(dmxx);
		addMessage(redirectAttributes, "保存弹幕信息成功");
		return "redirect:"+Global.getAdminPath()+"/zhtj/dmxx/?repage";
	}
	
	@RequiresPermissions("zhtj:dmxx:edit")
	@RequestMapping(value = "delete")
	public String delete(Dmxx dmxx, RedirectAttributes redirectAttributes) {
		dmxxService.delete(dmxx);
		addMessage(redirectAttributes, "删除弹幕信息成功");
		return "redirect:"+Global.getAdminPath()+"/zhtj/dmxx/?repage";
	}

	@RequestMapping("getDmInfo")
	public String getDmInfo(Dmxx dmxx,HttpServletResponse response){
		dmxxService.save(dmxx);
		List<Dmxx> contentList=dmxxService.getDmInfo();
		if(contentList!=null&&contentList.size()>0){
			this.renderString(response, ServerResponse.createBySuccess("获取弹幕信息成功",contentList));
			//TODO: 2017/8/23
			dmxxService.deleteById(contentList);
			return null;
		}
		return this.renderString(response,ServerResponse.createBySuccess("目前尚无弹幕",contentList));
	}
}